package com.example.mypra1;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 性格测试结果
 */
public class ResultActivity extends AppCompatActivity {
    private TextView career,xinge_summary,xinge_desc,character1,desc1,character2,desc2;
    private String answer;
    private Handler handler=new Handler(){

        @Override
        public void handleMessage(@NonNull Message msg) {//操作UI,渲染数据
            Map<String,Object> map = (Map<String, Object>) msg.obj;
            career.setText(map.get("career").toString());
            xinge_summary.setText(map.get("xinge_summary").toString());
            xinge_desc.setText(map.get("xinge_desc").toString());
            character1.setText(map.get("character1").toString());
            desc1.setText(map.get("desc1").toString());
            character2.setText(map.get("character2").toString());
            desc2.setText(map.get("desc2").toString());
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);
        career = findViewById(R.id.career);
        xinge_summary = findViewById(R.id.xinge_summary);
        xinge_desc = findViewById(R.id.xinge_desc);
        character1 = findViewById(R.id.character1);
        character2 = findViewById(R.id.character2);
        desc1 = findViewById(R.id.desc1);
        desc2 = findViewById(R.id.desc2);
        Intent intent = getIntent();//获取PersonalityActivity传参
        answer = intent.getStringExtra("answer");
        getData();//请求数据
    }
    //发送网络请求，获取测试结果
    private void getData(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient okHttpClient = new OkHttpClient();
                    Request request = new Request.Builder()
                            .url("http://apis.juhe.cn/fapig/character_test/analysis?answers="+answer+"&key=f9c99bfe72cf73e7c03d317bcd3c191a")
                            .build();
                    okHttpClient.newCall(request).enqueue(new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {

                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException,RuntimeException {
                            Log.d("resp",response.toString());
                            if (response.isSuccessful()) {
                                String res = response.body().string();
                                Log.d("res",res);
                                try {
                                    JSONObject jsonObject1 = new JSONObject(res);
                                    JSONObject jsonObject = jsonObject1.getJSONObject("result");
                                    Map<String,String>map = new HashMap<>();
                                    map.put("career",jsonObject.getString( "occupation"));
                                    map.put("xinge_summary",jsonObject.getJSONArray( "summarize").getString(0));
                                    map.put("xinge_desc",jsonObject.getJSONArray("desc").getString(0));
                                    JSONArray jsonArray = jsonObject.getJSONArray( "characteristic");
                                    JSONObject jsonObject2 = jsonArray.getJSONObject(0);
                                    JSONObject jsonObject3 = jsonArray.getJSONObject(1);
                                    map.put("character1",jsonObject2.getString("title"));
                                    map.put("desc1",jsonObject2.getJSONArray("desc").getString(0));
                                    map.put("character2",jsonObject3.getString("title"));
                                    map.put("desc2",jsonObject3.getJSONArray("desc").getString(0));
                                    Message message = new Message();
                                    message.obj = map;
                                    handler.sendMessage(message);

                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }else{
                                Log.d("resp",response.toString());
                            }
                        }
                    });

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}